<script>
import { get as _get } from "lodash";
import { decrypt } from "@/utils/encryption.js"
import { dateFormat, isEmptyData, getRiskDetailsDispose } from "@/utils";
/**
 * 司法协助执行人类型
 */
const EXECUTED_PERSON_TYPE_MAP = {
  2: "人",
  1: "公司",
};

/**
 * 失信执行人类型
 */
const DISRUPT_TYPE_MAP = {
  0: "人",
  1: "公司",
};

const RISK_TYPE_COLUMN_3 = 3,
  RISK_TYPE_COLUMN_8 = 8,
  RISK_TYPE_COLUMN_71 = 71,
  RISK_TYPE_COLUMN_12 = 12,
  RISK_TYPE_COLUMN_19 = 19,
  RISK_TYPE_COLUMN_21 = 21,
  RISK_TYPE_COLUMN_75 = 75,
  RISK_TYPE_COLUMN_13 = 13,
  RISK_TYPE_COLUMN_27 = 27,
  RISK_TYPE_COLUMN_9 = 9,
  RISK_TYPE_COLUMN_77 = 77,
  RISK_TYPE_COLUMN_29 = 29,
  RISK_TYPE_COLUMN_32 = 32,
  RISK_TYPE_COLUMN_73 = 73;

const CASE_STATUS = "案件身份";

export default {
  name: "DetailsIndex",
  props: {
    title: {
      type: String,
    },
    detailsObj: {
      type: Object,
    },
  },
  methods: {
    /**
     * 创建元素
     * @param {*} h 创建虚拟dom函数
     * @param {*} obj 详情数据对象
     */
    createElement(h, obj) {
      //类型
      const TYPE = this.detailsObj?.type;

      //获取表格字段信息
      const RISK_TABLE = getRiskDetailsDispose(this.title, TYPE);

      //循环字段表格创建元素
      const $createElement = RISK_TABLE.map((item) => {
        //获取字段值
        const $spanObj = {
          value: [isEmptyData(_get(obj, item.field))],
        }

        //以下if是为了判断各种风险类型，根据不同类型显示的字段也不同

        //是否需要格式化日期
        if (item?.isDate) {
          $spanObj.value = [dateFormat(_get(obj, item.field))];
        } else if (item?.timeStampNumber) {
          //将时间戳转为Number
          $spanObj.value = [dateFormat(Number(_get(obj, item.field)))];
        } else if (
          TYPE == RISK_TYPE_COLUMN_12 ||
          TYPE == RISK_TYPE_COLUMN_19 ||
          this.title?.endsWith("变更")
        ) {
          //是否是预警提示信息
          $spanObj.value = [
            {
              domProps: {
                innerHTML: isEmptyData(_get(obj, item.field)),
              },
            },
          ];
        } else if (
          (TYPE == RISK_TYPE_COLUMN_3 || TYPE == RISK_TYPE_COLUMN_71) &&
          item.field == "type"
        ) {
          //失信被执行人、历史失信被执⾏⼈ 失信⼈类型转换
          $spanObj.value = [isEmptyData(DISRUPT_TYPE_MAP[_get(obj, item.field)])];
        } else if (
          TYPE == RISK_TYPE_COLUMN_21 &&
          item.field == "executedPersonType"
        ) {
          //司法协助(公司) 执行人类型转换
          $spanObj.value = [
            isEmptyData(EXECUTED_PERSON_TYPE_MAP[_get(obj, item.field)]),
          ];
        } else if (TYPE == RISK_TYPE_COLUMN_13 && item.title == CASE_STATUS) {
          //开庭公告 案件身份显示调整
          $spanObj.value = [
            item.field.map((n) => {
              const value = _get(obj, n.field);
              const dataVal =
                n.field == "plaintiff"
                  ? isEmptyData(this.getPlaintiffInfo(value))
                  : value;
              return h("p", [h("span", `${n.title}：`), h("span", dataVal)]);
            }),
          ];
        } else if (TYPE == RISK_TYPE_COLUMN_27 && item.title == CASE_STATUS) {
          //立案信息 案件身份显示调整
          $spanObj.value = [
            item.field.map((n) => {
              return h("p", [
                h("span", `${n.title}：`),
                h("span", isEmptyData(_get(obj, n.field))),
              ]);
            }),
          ];
        } else if (
          (TYPE == RISK_TYPE_COLUMN_9 || TYPE == RISK_TYPE_COLUMN_77) &&
          item.field == "targetCompany"
        ) {
          //股权出质 出质股权标的企业字段判断
          $spanObj.value = [
            isEmptyData(_get(obj, item.field[0])) ||
              isEmptyData(_get(obj, item.field[1])),
          ];
        } else if (TYPE == RISK_TYPE_COLUMN_71 && item.field == "staff") {
          //历史失信被执⾏⼈ 法定负责⼈/主要负责⼈显示信息调整
          $spanObj.value = [
            isEmptyData(
              _get(obj, item.field)
                ?.map((s) => {
                  return `${s.name}（${s.role}）`;
                })
                .join(",")
            ),
          ];
        } else if (TYPE == RISK_TYPE_COLUMN_29 && item.field == "detail") {
          //司法拍卖 拍卖物显示信息调整
          $spanObj.value = [
            _get(obj, item.field)?.map((dt) => {
              return h(
                "p",
                `标的名称：${dt.title}，评估价：${dt.evaluationPrice}，起拍价：${dt.startingPrice}`
              );
            }),
          ];
        } else if (
          (TYPE == RISK_TYPE_COLUMN_32 && item.field == "ossPath") ||
          (TYPE == RISK_TYPE_COLUMN_73 && item.field == "filePath") ||
          (TYPE == RISK_TYPE_COLUMN_8 && item.field == "lawsuitUrl")
        ) {
          //限制消费令 pdf、天眼查页面地址
          const url = _get(obj, item.field);
          const isPageUrl = item.field == "lawsuitUrl";
          $spanObj.value = url
            ? [
                {
                  style: { color: "#429df8", "word-break": "break-all" },
                  on: {
                    click: () => {
                      //法律诉讼跳转页面
                      if (isPageUrl) {
                        window.location.href = url;
                      } else {
                        MXCommon.download(url);
                      }
                    },
                  },
                },
                isPageUrl ? "点击查看" : "下载",
              ]
            : [decrypt(process.env.VUE_APP_DEFAULT_SHOW_TEXT)];
        }

        return h("p", [h("span", item.title), h("span", ...$spanObj.value)]);
      });

      return $createElement;
    },

    /**
     * 获取原告信息
     * @param {*} plaintiff 原告数据
     */
    getPlaintiffInfo(plaintiff) {
      return plaintiff?.map((i) => i.name)?.join(",");
    },
  },
  render(h) {
    return h(
      "div",
      { class: "details-index-contianer" },
      this.detailsObj?.dataList?.map((obj, index) => {
        return h("div", { class: "risk-sector-item-info" }, [
          h("p", [h("span", "序号"), h("span", index + 1)]),
          this.createElement(h, obj),
        ]);
      })
    );
  },
};
</script>